<?php

include("global.php");

$pagetitle=" - Administration - Users";
$links=" > Administration > Users";

include ("header.php");

include ("admin_header.php");

if($userloggedinarray[15]!=="administrator"){
   unset($tplcontent);
   $tplcontent['notice']="Must be logged in as administrator to use control panel!";
   template('notice',$tplcontent);
}else{

if(!$edituser&&!$edituser2&&!isset($ban)&&!$unban&&!$action&&!isset($showuser)){

  unset($tplcontent);

  $tplcontent['formheader'].="<form action=\"admin_users.php\" method=get>";

  $tplcontent['adminusersmenu'].="<select size=1 name=\"showuser\" class=\"forminput\">";
  @sort($usersarray,SORT_NUMERIC);
  for($n=0;$n<count($usersarray);$n++){
  	$userarray=getdata("$configarray[1]/$usersarray[$n]/main.php");
  	$username=$userkeyarray[$usersarray[$n]];
  	$tplcontent['adminusersmenu'].="<option value=\"$usersarray[$n]\">$usersarray[$n], $userarray[0], $username</option>";
  }
  $tplcontent['adminusersmenu'].="</select>";

  $tplcontent['submitbutton'].="<input type=submit name=\"submit\" value=\"Continue\" class=\"formbutton\">";
  $tplcontent['formfooter'].="</form>";

  echo template('adminuserslist',$tplcontent);
}


if(isset($showuser)){

    $userarray=getdata("$configarray[1]/$showuser/main.php");

    if(count($userarray)>0){
    unset($tplcontent);

    $username=$userkeyarray[$showuser];

    $tplcontent['userinfo'].="Username: $username, Displayname: $userarray[0], UserID: $showuser";

    if(@in_array($showuser,$banarray)){
    	$tplcontent['useridban'].="<a href=\"admin_users.php?unban=$showuser\">Unban by userid</a>";
    }else{
    	$tplcontent['useridban'].="<a href=\"admin_users.php?ban=$showuser\">Ban by userid</a>";
    }

    if(@in_array($userarray[19],$banarray)){
    	$tplcontent['useripban'].="<a href=\"admin_users.php?unban=$userarray[19]\">Unban by IP</a>";
    }else{
    	$tplcontent['useripban'].="<a href=\"admin_users.php?ban=$userarray[19]\">Ban by IP</a>";
    }

    $emailarray=explode("\t",$userarray[2]);

    if(@in_array($emailarray[0],$banarray)){
    	$tplcontent['useremailban'].="<a href=\"admin_users.php?unban=$emailarray[0]\">Unban by email</a>";
    }else{
    	$tplcontent['useremailban'].="<a href=\"admin_users.php?ban=$emailarray[0]\">Ban by email</a>";
    }

    $tplcontent['updatepostcount'].="<a href=\"admin_users.php?action=updatepostcount&user=$showuser\">Update post count to what is on forum</a>";
    
    $tplcontent['deleteuser'].="<a href=\"admin_users.php?action=deleteuser&user=$showuser\">Delete this user (Account will not be recoverable)</a>";
    
	$tplcontent['commonoptions'].="For more common options like profile details and passwords and buddy lists, ";
	$tplcontent['commonoptions'].="<a href=\"user_edit.php?user=$showuser\">edit the user</a> normally through the forum";
	
    $tplcontent['formheader'].="<form action=\"admin_users.php\" method=post>";
    $tplcontent['formheader'].="<input type=hidden name=\"edituser\" value=\"$showuser\" size=40>";

	$tplcontent['regtimestamp'].="<input type=text name=\"registeredtime\" value=\"$userarray[4]\" class=\"forminput\" size=\"40\">";

	$tplcontent['previousandrecenttime'].="<input type=text name=\"previousandrecenttime\" value=\"$userarray[5]\" class=\"forminput\" size=\"40\">";

	$tplcontent['postcount'].="<input type=text name=\"postcount\" value=\"$userarray[6]\" class=\"forminput\" size=\"40\">";

    $tplcontent['usergroup'].="<select name=\"usergroup\" class=\"forminput\">";
	
	if($userarray[15]=="registered")
	{$tplcontent['usergroup'].="<option value=\"registered\" selected>registered</option>";}
	else{$tplcontent['usergroup'].="<option value=\"registered\">registered</option>";}
	
	if($userarray[15]=="administrator")
	{$tplcontent['usergroup'].="<option value=\"administrator\" selected>administrator</option>";}
	else{$tplcontent['usergroup'].="<option value=\"administrator\">administrator</option>";}
	
    if($userarray[15]=="confirm")
	{$tplcontent['usergroup'].="<option value=\"confirm\" selected>confirm</option>";}
	else{$tplcontent['usergroup'].="<option value=\"confirm\">confirm</option>";}
	
	$usergroupsarray=getdata("$configarray[1]/usergroups.php");
	 for($n=0;$n<count($usergroupsarray);$n++){
	  if($userarray[15]==$usergroupsarray[$n])
	  {$tplcontent['usergroup'].="<option value=\"$usergroupsarray[$n]\" selected>$usergroupsarray[$n]</option>";}
	  else{$tplcontent['usergroup'].="<option value=\"$usergroupsarray[$n]\">$usergroupsarray[$n]</option>";}
	 }
	$tplcontent['usergroup'].="</select>";

	$tplcontent['lastpost'].="<input type=text name=\"lastposttime\" value=\"$userarray[18]\" class=\"forminput\" size=\"40\"><br>";

	$tplcontent['lastip'].="<input type=text name=\"lastip\" value=\"$userarray[19]\" class=\"forminput\" size=\"40\"><br>";
	
    $tplcontent['submitbutton'].="<input type=submit name=\"submit\" value=\"Update\" class=\"formbutton\">";
    $tplcontent['formfooter'].="</form>";
	
    echo template('adminuserview',$tplcontent);
    
	}else{
		unset($tplcontent);
    	$tplcontent['notice'].="User does not exist";
    	echo template('notice',$tplcontent);
    }

 }
 
 if($edituser){
  writedata("$configarray[1]/$edituser/main.php",$registeredtime,4);
  writedata("$configarray[1]/$edituser/main.php",$previousandrecenttime,5);
  writedata("$configarray[1]/$edituser/main.php",$postcount,6);
  writedata("$configarray[1]/$edituser/main.php",$usergroup,15);
  writedata("$configarray[1]/$edituser/main.php",$lastposttime,18);
  writedata("$configarray[1]/$edituser/main.php",$lastip,19);

  unset($tplcontent);
  $tplcontent['notice']="User edited<br><br><a href=\"admin_users.php\">Back to listing</a><br><br><a href=\"admin_users.php?showuser=$edituser\">Back to user</a>";
  echo template('notice',$tplcontent);
 }

 if(isset($ban)){
 
 unset($tplcontent);
 
 if(!@in_array($ban,$banarray)){
 writedata("$configarray[1]/ban.php",$ban,count($banarray));
 $tplcontent['notice'].="'$ban' has been added to ban list";
 }else{
 $tplcontent['notice'].="'$ban' is already in ban list";
 }
 echo template('notice',$tplcontent);
 }

 if(isset($unban)){

 for($n=0;$n<count($banarray);$n++){

  if($banarray[$n]==$unban){
  deletedata("$configarray[1]/ban.php",$n);
  }

 }

 unset($tplcontent);
 $tplcontent['notice'].="'$unban' has been removed from ban list";
 echo template('notice',$tplcontent);

 }

if($action=="updatepostcount"){
$posts=0;

 for($m=0;$m<count($forumarray);$m++){
 $forumconfigarray=getdata("$configarray[2]/$forumarray[$m].php");

  if($forumconfigarray[6]=="on"){
  $threadarray=listdirs("$configarray[2]/$forumarray[$m]");

  for($n=0;$n<count($threadarray);$n++){
   $postarray=listfiles("$configarray[2]/$forumarray[$m]/$threadarray[$n]");
    
   for($l=0;$l<count($postarray);$l++){
    $indpostarray=getdata("$configarray[2]/$forumarray[$m]/$threadarray[$n]/$postarray[$l].php");

    if($indpostarray[0]=="$user"){
     $posts++;
    }

   }//post loop

  }//thread loop

  }//post increase check

}//forum loop

writedata("$configarray[1]/$user/main.php",$posts,6);

 unset($tplcontent);
 $tplcontent['notice']="User '$user' post count has been recounted at '$posts' posts";
 echo template('notice',$tplcontent);

}

if($action=="deleteuser"&&$user){
	deleteuser($user);
	
	unset($tplcontent);
	$tplcontent['notice'].="User '$user' has been deleted";
	echo template('notice',$tplcontent);
}

}

include("admin_footer.php");

include("footer.php");

?>
